<?php
/**
 * 发送单人奖励
 */

require_once('../inc/config.php');
require_once('../inc/class/socket_class.php');
//权限判断
chkpurview('single_reward');

$action = isset($_GET['action']) ? $_GET['action'] : null;

if($action == 'check')							//ajax验证用户账号是否存在
{
	$accounts = explode("\n",$_GET['accounts']);
	$type = trim($_GET['type']);
	foreach($accounts as  $k=>$v)
	{
		$type == 'loginname' ? $sql = "select id from tb_user where loginname = '$v' limit 1" : $sql = "select id from tb_player where BINARY nickname = '$v' limit 1";
		$rs = $db->query_first($sql);
		if(empty($rs))
		{
			$unexist_accounts[] = $v;
		}
	}
	
	if(empty($unexist_accounts))
	{
		exit;		//账号都存在
	}
	else 
	{
		echo json_encode($unexist_accounts);	//返回不存在的账号
		exit;
	}
}
else if($action== 'send')										//提交表单
{
	$emailtitle = trim($_POST['emailtitle']);
	if (empty($emailtitle)) 
	{
		gourl($_LANG['fill_incomplete'], '', -1);	
	}
	
	if (strlen($emailtitle) > 36)
	{
		gourl($_LANG['email_title_too_long'], '', -1);	
	}
	
	$accounts = isset($_POST['nickname']) ? $_POST['nickname'] : $_POST['loginname'];
	if(isset($_POST['nickname']))
	{
		$info['account_array'] = explode("\n",stripcslashes($_POST['nickname']));
		foreach($info['account_array'] as $k=>$v)
		{
			$info['account_array'][$k] = trim($v);
			$sql = "SELECT id FROM tb_player WHERE nickname = '$v'";
			$result = $db->query($sql);		
			while($r = $db->fetch_array($result))		
			{
				$info['account_array'][$k] = $r['id'];
			}
		}
	}
	else if(isset($_POST['loginname']))
	{
		$info['account_array'] = explode("\n",stripcslashes($_POST['loginname']));
		foreach($info['account_array'] as $k=>$v)
		{
			$info['account_array'][$k] = trim($v);
			$sql = "SELECT id FROM tb_user WHERE loginname = '$v'";
			$result = $db->query($sql);		
			while($r = $db->fetch_array($result))		
			{
				$info['account_array'][$k] = $r['id'];
			}
		}
	}

	$isBind = isset($_POST['isBind'])?$_POST['isBind']:null;	//0不绑定,1绑定(默认)
	$itemtype 	= $_POST['itemtype'];
	$itemid 	= $_POST['itemid'];
	$itemnum 	= $_POST['itemnum'];

	$rewards = NULL;
	foreach($itemtype as $k => $v)
	{
		if($v == 0)
		{
			continue;
		}
		elseif($v == 1)
		{						//道具
			$temp['type'] = 0x2008;
			$temp['value'] = intval($itemid[$k]<<16)+ intval($isBind[$k]<<8) + intval($itemnum[$k]);
			$rewards[] = $temp;
		}
		elseif($v == 2)
	{							//元宝
			$temp['type'] = 0x1006;
			$temp['value'] = $itemnum[$k];
			$rewards[] = $temp;
		}
		elseif($v == 3)
		{						//礼金
			$temp['type'] = 0x1007;
			$temp['value'] = $itemnum[$k];
			$rewards[] = $temp;
		}
		elseif($v == 4)
		{						//铜币
			$temp['type'] = 0x1004;
			$temp['value'] = $itemnum[$k];
			$rewards[] = $temp;
		}
		elseif($v == 5)
		{						//绑定铜币
			$temp['type'] = 0x1005;
			$temp['value'] = $itemnum[$k];
			$rewards[] = $temp;
		}
	}

	$info['emailtitle'] 			= $emailtitle;											//邮件标题
	$info['emailcontent'] 	= trim($_POST['emailcontent']);			//邮件内容
	$info['sendtitle'] 			= trim($_POST['sendtitle']);					//奖励原因
	$info['rewards'] 				= $rewards;												//奖励物品

	/*--------------插入数据库信息--------------*/
	$oper_type = 2;
	foreach ($rewards as $v){
		$rewards_temp[] = $v['type'].'.'.$v['value'];
	}
	$rewards_value = implode(":", $rewards_temp);
	unset($rewards_temp);
	
	if (isset($_POST['loginname'])){
		$object_type = 1;
		$object = implode(':', $info['account_array']);
	}elseif (isset($_POST['nickname'])){			
		$object_type = 0;
		$object = implode(':', $info['account_array']);
	}
	$oper_value = $info['emailtitle'].'#'.$info['emailcontent'].'#'.$object_type.'#'.$object.'#'.$info['sendtitle'].'#'.$rewards_value;
	/*-----------------------------------------*/
	save_adminoper($oper_type, $oper_value, 1);			//1：待审核
	
//	$action = 'single_reward';
//	$gamesocket = new gamesocket();
//	$socket = $gamesocket->create();
//	$gamesocket->connect($socket, $socket_gateway['address'], $socket_gateway['port']);
//	$header = merge_header($action);
//	$packmsg = $gamesocket->packmsg($header, $format, $info, $action);
//	$gamesocket->send($socket, $packmsg['buffer']);
//
////	print_r(unpack('H*', $packmsg['buffer']));exit;
//	if($recv = $gamesocket->recv($socket))
//	{
//		$gamesocket->close();
//		$msg=unpack(unpack_header_format().'h8retcode', $recv);
//		$msg['retcode'] = strrev($msg['retcode']);
//		if ($msg['retcode']=='00000000'){								//服务端返回成功信息
//			save_adminoper($oper_type, $oper_value, 0);
//			gourl($_LANG['operate_success'], 'single_reward.php');
//		}else {															//服务端返回错误信息
//			save_adminoper($oper_type, $oper_value, $msg['retcode']);
//			gourl($_LANG['operate_fail'], '', -1);
//		}
//	}
}

$db->close();

$giftArray = array(1,2,3,4,5,6,7);
$arrRes = array(
	0 => $_LANG['none'],
	1 => $_LANG['daoju'],
	2 => $_LANG['gold'],
	3 => $_LANG['bind_gold'],
	4 => $_LANG['copper'],
	5 => $_LANG['bind_copper']
);
$T->assign("arrRes",$arrRes);
$T->assign('giftArray',$giftArray);
$T->assign("script_name",$_SERVER['SCRIPT_NAME']);
$T->display('single_reward.html');


